import axios from "axios";
import { useSelector } from "react-redux";
import { useNavigate } from "react-router-dom";

const servicer = axios.create({
    //baseURL:"http://xue.cnkdl.cn:23683",
    baseURL:"http://tech.wolfcode.cns",
    timeout: 5000
})

// 请求拦截器
servicer.interceptors.request.use((config) => {
    const { userInfo } = useSelector(state => state.userReducer)
    if( userInfo.token ) {
        config.headers.Authorization = userInfo.token
    }
    return config
},(error) => {
    return Promise.reject(error)
})

// 相应拦截器
servicer.interceptors.response.use((response) => {

    return response.data
},(error) => {
    const navigate = useNavigate()
    if (error.response?.status === 401) {
        navigate('/login')
    }
    return Promise.reject(error)
})

export default servicer